import { getRoleList } from '@/api/system/role';
import {modifyUserStatus} from '@/api/system/platformuser'
import { BasicColumn } from '@/components/Table';
import { FormSchema } from '@/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { useMessage } from '@/hooks/web/useMessage';

type CheckedType = boolean | string | number;
export const columns: BasicColumn[] = [
  // {
  //   title: '所属地区',
  //   dataIndex: 'regionName',
  //   customRender: ({ record }) => {
  //     return record.provinceName + (record.cityName ? '/' + record.cityName : '');
  //   }
  // },
  {
    title: '用户名',
    dataIndex: 'account',
    slots: { customRender: 'no' },
  },
  {
    title: 'UserId',
    dataIndex: 'userId',

  },
  {
    title: '手机号码',
    dataIndex: 'phoneNum',

  },
  {
    title: '邮箱',
    dataIndex: 'email',

  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    customRender: ({ record }) => {
      //将时间戳转换位时间
      const date = new Date(record.createTime);
      const Y = date.getFullYear() + '-';
      const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      const D = date.getDate() + ' ';
      const h = date.getHours() + ':';
      const m = date.getMinutes() + ':';
      const s = date.getSeconds();
      return Y + M + D + h + m + s;
    }
  },
  {
    title: '角色',
    dataIndex: 'role.roleName',
    customRender: ({ record }) => {
      return record.role.roleName;
    }
  },

  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    customRender: ({ record }) => {
      console.log(record)
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          const { createMessage } = useMessage();
          if (record.isMain == 1){
            createMessage.error('主账号不允许禁用');
            record.pendingStatus = false;
            return;
          }
          modifyUserStatus(record.userId, newStatus)
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功修改用户状态`);
            })
            .catch(() => {
              createMessage.error('修改用户状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  }
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'account',
    label: '用户名',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'phoneNum',
    label: '手机号',
    component: 'Input',
    colProps: { span: 8 },
  },
];

export const accountFormSchema: FormSchema[] = [
  {
    field: 'account',
    label: '用户名',
    component: 'Input',
    // helpMessage: ['本字段演示异步验证', '不能输入带有admin的用户名'],
    rules: [
      {
        required: true,
        message: '请输入用户名',
      },
      // {
      //   validator(_, value) {
      //     // if(account == value) return true;
      //     return new Promise((resolve, reject) => {
      //       isAccountExist(value)
      //         .then(() => resolve())
      //         .catch((err) => {
      //           reject(err.message || '验证失败');
      //         });
      //     });
      //   },
      // },
    ],
    colProps: { span: 24 },
  },
  {
    field: 'Password',
    label: '密码',
    component: 'InputPassword',
    required: true,
    ifShow: true,
    colProps: { span: 24 },
  },
  {
    field: 'confirmPassword',
    label: '重复密码',
    component: 'InputPassword',
    required: false,
    ifShow: true,
    colProps: { span: 24 },
  },
  {
    label: '角色',
    field: 'roleId',
    component: 'ApiSelect',
    componentProps: {
      api: getRoleList,
      labelField: 'roleName',
      valueField: 'id',
    },
    required: true,
    colProps: { span: 24 },
  },
  {
    field: 'deptName',
    label: '所属部门',
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'name',
        value: 'value',
      },
      getPopupContainer: () => document.body,
    },
    required: true,
    colProps: { span: 24 },
  },
  // {
  //   field: 'province',
  //   component: 'ApiSelect',
  //   label: '所属省',
  //   required:true,
  //   componentProps:  ({ formModel, formActionType }) => {
  //     return {
  //       api: getProvince,
  //       labelField: 'regionName',
  //       valueField: 'regionId',
  //       placeholder: '选择地区',
  //       showSearch:true,
  //       onChange: async (e: any) => {
  //         var citiesOptionsData = await getCity({regionCode:e});
  //         var point_arr = citiesOptionsData.data
  //         formModel.city = undefined; //  reset city value
  //         const { updateSchema } = formActionType;
  //         updateSchema({
  //           field: 'city',
  //           componentProps: {
  //             options: point_arr,
  //           },
  //         });
  //       },
  //     };
  //   },
  // },
  // {
  //   field: 'city',
  //   component: 'Select',
  //   label: '所属市',
  //   componentProps: {
  //     placeholder: '选择所属市（省级账号不选）',
  //     showSearch:true,
  //     label: 'regionName',
  //     value: 'regionId',
  //   },
  // },
  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
    required: true,
    colProps: { span: 24 },
  },

  {
    label: '手机号',
    field: 'phoneNum',
    component: 'Input',
    required: true,
    colProps: { span: 24 },
  },
];
